#include<iostream>

using namespace std;

struct ListNode {
	int val;
	ListNode *next;
	ListNode() : val(0), next(nullptr) {}
	ListNode(int x) : val(x), next(nullptr) {}
	ListNode(int x, ListNode *next) : val(x), next(next) {}
};

class Solution {
public:
	ListNode* removeElements(ListNode* head, int val) {
		while(head&&head->val == val)
			head = head->next;
		if (!head) return NULL;
		ListNode *r=head,*temp=head->next;
		while (temp) {
			if (temp->val == val) {
				r->next = temp->next;
				temp = temp->next;
			}
			else {
				r = temp;
				temp = temp->next;
			}	
		}
		return head;
	}
};

void main() {
	ListNode n1(2), n2(2), n3(2), n4(2), n5(2);
	n1.next = &n2, n2.next = &n3, n3.next = &n4, n4.next = &n5;
	Solution s;
	s.removeElements(&n1,2);
}